$(function()
{
	$(".delete-product").submit(function(e){
		return confirm("WARNING: This action will DELETE this product!\n\nAre you sure you want to proceed?");
	});

	$("#name.newproduct").blur(function(event) {
		$.post("/manager/products/getslug", {
			"str":$(this).val()
		}, function(data)
		{
			$("#slug").val(data.val);
		})
	});
	
	$("#product-shortdescription-editor").ckeditor({
		"toolbar":"Basic",
		"height":"100px"
	});
	
	$("#product-description-editor").ckeditor({
		"removeButtons":"Save"
	});
	
	$("#addattributebutton").expire().livequery(function()
	{
		$(this).click(function(e)
		{
			e.preventDefault();
			var $clone = $("#newattributetemplate").html();
			
			$clone = $clone.replace(/!/gi,"");
			$clone = $clone.replace(/@num@/gi, ($(".customattribute").size() + 1) );
			
			if($("#newattribute").val() != "")
			{
				$clone = $clone.replace(/@attributename@/gi, $("#newattribute").val() );
				$clone = $clone.replace(/@attributeid@/gi, "-1" );
			}
			else
			{
				$clone = $clone.replace(/@attributename@/gi, $("#productattribute > option:selected").text() );
				$clone = $clone.replace(/@attributeid@/gi, $("#productattribute > option:selected").val() );
			}
			
			$("#addattributerow").before($clone);
			
			$("#numattributes").val($(".customattribute").size());
			
		});
	});
	
	$(".attributecontent").expire().livequery(function()
	{
		$(this).ckeditor({
			"removeButtons":"Save"
		});
	});
	
	$(".removeattributebutton").expire().livequery(function()
	{
		$(this).click(function(e)
		{
			e.preventDefault();
			if(confirm("Sure?"))
			{
				$(this).parent().parent().remove();
				$("#numattributes").val($(".customattribute").size());
			}
		});
	});
	
	$("body").on("click", ".btn.deleteproductimage", function(e)
	{
		e.preventDefault();
		var $this = $(this), $parent = $(this).parents(".imageuploaditem");
		
		if(confirm("Are you sure you want to delete this image?"))
		{
			$.get($this.data("url"), function()
			{
				$parent.fadeOut("fast", function()
				{
					$(this).remove();
				});
			});
		}
	});
	
	$("body").on("click", ".btn.deleteproductfile", function(e)
	{
		e.preventDefault();
		var $this = $(this), $parent = $(this).parents(".fileuploaditem");
		
		if(confirm("Are you sure you want to delete this file?"))
		{
			$.get($this.data("url"), function()
			{
				$parent.fadeOut("fast", function()
				{
					$(this).remove();
				});
			});
		}
	});
	
	$(".categorylistitem").on("click", function()
	{
		var cb = $(this).find("input[type=checkbox]");
		cb.click();
		$(this).toggleClass("selected", (cb.is(":checked")));
	});
	
	$(document).bind('drop dragover', function (e) {
		e.preventDefault();
	});
	
	
	$("#product-filter").on("keyup", function(e)
	{
		$("#available-products .product-list-item").addClass("hide");
		if($(this).val() != "")
		{
			var re =  RegExp($(this).val() ,"i");
			$("#available-products .product-list-item")
				.filter(function()
				{
					return re.test($(this).attr("data-productname"));
				})
				//.filter("[data-productname*="+$(this).val()+"]")
				.removeClass("hide")
				.find("img").trigger("unveil");
		}
	});
	
	$( "#available-products, #related-products" ).sortable({
		connectWith: ".connectable",
		revert: 200,
		start: function(event, ui)
		{
			$(".connectable").addClass("highlight").sortable( "refresh" );
		},
		stop: function(event, ui)
		{
			$(".connectable").removeClass("highlight").sortable( "refresh" );
			
			var $relatedproducts = $("#related-products").find("> li").map(function(i, el)
			{
				return parseInt($(el).data("relatedproductid"));

			}).get();
			
			$.post("/manager/products/saverelatedproducts",
			{
				"productid" : parseInt($("#related-products").data("productid")),
				"relatedproducts" : $relatedproducts
			}, function(data)
			{
				
			}, "json");
		}
	}).disableSelection();

	$("img").unveil();
});